package client.gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import packets.server.UserDetails;
import packets.server.UserList;

import client.logic.Controller;
import client.logic.Facade;
import client.logic.GuiFacade;

/**
 * main frame which can display various windows 
 */
public class frmMainFrame extends JFrame {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * runs the client
	 * @param args
	 */
	public static void main(String[] args){
		if (args.length>0)
			Controller.setServerIp(args[0]);
		GuiFacade.getInstance();
		//new MainFrame().setVisible(true);
	}

	/**
	 * Creates an instance of frmMainFrame
	 */
	public frmMainFrame(){
		changeScreen(new pnlLoginScreen());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosed(WindowEvent e){
				Facade.getInstance().closeChatStarter();
				Facade.getInstance().closeChatWaiter();
			}
			@Override
			public void windowClosing(WindowEvent e){
				Facade.getInstance().closeChatStarter();
				Facade.getInstance().closeChatWaiter();
			}
		});
	}

	/**
	 * changes the main content pane of the main frame , to display it on the screen
	 * @param p the panel to dispaly
	 */
	private void changeScreen(JPanel p){
		setContentPane(p);
		pack();
	}

	/**
	 * changes the screen into the login screen
	 */
	public void changeToLogin(){
		changeScreen(new pnlLoginScreen());
	}
	
	/**
	 * changes the screen into the all users view screen
	 */
	public void changeToAllUsers(UserList data){
		changeScreen(new pnlAllUsers(data));
	}
	
	/**
	 * changes the screen into the online users view screen
	 */
	public void changeToWhosOnline(UserList data){
		changeScreen(new pnlWhosOnline(data));
	}
	
	/**
	 * changes the display to the registration screen
	 */
	public void changeToRegister(){
		changeScreen(new pnlRegister());
	}
	/**
	 * changes the display into the user's panel screen
	 */
	public void changeToMainScreen(){
		changeScreen(new pnlMainScreen());
	}

	/**
	 * changes the display into the show details screen
	 * @param user the details to view
	 */
	public void changeToDetailsScreen(UserDetails user) {
		changeScreen(new pnlUserDetails(user));
	}

	/**
	 * opens a new error message dialog and displays it to the screen , with the given message
	 * @param msg the message to show on the dialog
	 */
	public void showErrorMessage(String msg) {
		JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
	}
}
